package cn.app.web.shiro;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import cn.app.dao.devuser.DevUserDao;
import cn.app.entity.DevUser;

public class CustomRealm extends AuthorizingRealm {
	@Autowired
	private DevUserDao devUserDao;
	
	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "CustomRealm";
	}
	
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(
			AuthenticationToken token) throws AuthenticationException {
		//获取用户名
		String devCode=(String)token.getPrincipal();
		
		//根据用户名去数据库查询
		DevUser devUser=devUserDao.findDevUserByDevCode(devCode);
		if(devUser == null){
			return null;
		}
		
		/*String password = (String)token.getCredentials();
		String devpassword = devUser.getDevpassword();
		if(devpassword.equals(password)){
				
			
			
		}*/
		
		SecurityUtils.getSubject().getSession().setAttribute("devUser", devUser);
		
		/*
		 * 
		 * 在异常处理器，如果捕获到，用户名和密码错误的异常
		 * SecurityUtils.getSubject().getSession().remove("devUser")
		 */
		
		
		
		return new SimpleAuthenticationInfo(devUser.getDevcode(),devUser.getDevpassword(),getName());
	}
	
	
	
	
	
	
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	

}
